#include "RelativeMouseController.h"
#include <Windows.h>
#include <iostream>
#include "Time.h"

#define SPEED_MULTIPLIER 0.000003
#define DEAD_ZONE 0.05

RelativeMouseController::RelativeMouseController(controllers::Joystick* cont):
	MouseController(cont) {
}


RelativeMouseController::~RelativeMouseController() {
}

void RelativeMouseController::run(){
	POINT mousePos;
	GetCursorPos(&mousePos);
	int currX = mousePos.x;
	int currY = mousePos.y;
	__int64 currTime = time::nanoTime();
	__int64 elapsedTime = currTime - lastTime;

	float xData = controller->getX();
	float yData = controller->getY();
	int newX;
	if (xData < -DEAD_ZONE || xData > DEAD_ZONE) {
		newX = (int) (xData * SPEED_MULTIPLIER * elapsedTime) + currX;
	} else {
		newX = currX;
	}
	int newY;
	if (yData < -DEAD_ZONE || yData > DEAD_ZONE) {
		newY = (int) (yData * SPEED_MULTIPLIER * elapsedTime) + currY;
	} else {
		newY = currY;
	}
	if (newX != currX || newY != currY) {
		robot.mouseMove(newX, newY);
	}
	lastTime = currTime;
}
